package com.sure0000.basicKafka.example;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * @author xuyouchang
 * @date 2020/8/12.
 */
public class ConnectorStatus {
    public static void main(String[] args) throws IOException {
        HttpClient httpClient = HttpClients.createDefault();
        HttpGet get = new HttpGet("http://10.10.77.152:18083/connectors");
        HttpResponse response = httpClient.execute(get);
        String result = EntityUtils.toString(response.getEntity(), "utf-8");
        result = result.replace("[", "").replace("]","").replace("\"", "");
        List<String> connectors = Arrays.asList(result.split(","));
        connectors.forEach((connector) -> {
            String url = "http://10.10.77.152:18083/connectors/"+ connector +"/status";
            HttpGet httpGet = new HttpGet(url);
            try {
                HttpResponse httpResponse = httpClient.execute(httpGet);
                JSONObject statusObject = JSONObject.parseObject(EntityUtils.toString(httpResponse.getEntity(), "utf-8"));
                String taskStatus = statusObject.getJSONArray("tasks").getJSONObject(0).getString("state");
                if ("failed".equalsIgnoreCase(taskStatus)) {
                    System.out.println("connector is faild：" + connector);
                }
            } catch (Exception e) {
//                System.out.println("parse connector " + connector + " failed !");
            }
        });

    }
}
